package com.mcxx.modules.miniSalvation.order.service.read;

import com.mcxx.modules.base.service.read.AreaHelper;
import com.mcxx.modules.base.utils.QueryParamUtil;
import com.mcxx.modules.miniSalvation.fundpay.param.BlacklistParam;
import com.mcxx.modules.miniSalvation.order.dao.read.BlacklistReadDao;
import com.mcxx.modules.miniSalvation.order.dto.BlacklistDTO;
import com.mcxx.modules.miniSalvation.order.entity.BlacklistEntity;
import com.mcxx.util.CommonUtil;
import com.yinhai.ta404.component.org.orguser.area.entity.TaAreaPo;
import com.yinhai.ta404.component.org.orguser.area.service.read.AreaManagementReadService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@TaTransactional(readOnly = true)
@Service
public class BlacklistReadService {
    @Autowired
    private BlacklistReadDao blacklistReadDao;
    @Autowired
    private AreaHelper areaHelper;
    @Autowired
    private AreaManagementReadService areaManagementReadService;

    public Page blackList(BlacklistParam param, PageParam pageParam, UserAccountVo userAccountVo) {

        blacklistReadDao.beginPager(pageParam);
        String tokenAreaCode=QueryParamUtil.setAreaCodeByLevel(userAccountVo.getAreaCode(), Integer.valueOf(userAccountVo.getAreaLevel()));
        String addressCode="";
        if(!CommonUtil.invalidArgs(param.getAddressCode())){
            TaAreaPo taAreaPo=areaManagementReadService.queryAreaByAreaCode(param.getAddressCode());
           addressCode=QueryParamUtil.setAreaCodeByLevel(param.getAddressCode(),taAreaPo.getAreaLevel());
        }
        param.setTokenAreaCode(tokenAreaCode);
        param.setAddressCode(addressCode);
        List<BlacklistDTO>list=blacklistReadDao.blackList(param);
        for (BlacklistDTO blacklistDTO:list){
            if (!CommonUtil.invalidArgs(blacklistDTO.getAreaCode())) {
                AreaHelper.AreaPo areaPo= areaHelper.getAreaByCode(blacklistDTO.getAreaCode());
                blacklistDTO.setStreet(areaPo.getStreet());
                blacklistDTO.setCommunity(areaPo.getCommunity());
            }
        }
        return  blacklistReadDao.endPager(list);
    }


    public int cntByIdcard(String idcard){
        return blacklistReadDao.cntByIdcard(idcard);
    }


    public List<String> cntByIdcards(List<String> idCardList){
        return blacklistReadDao.cntByIdcards(idCardList);
    }


    public BlacklistEntity getByIdCard(String idcard) {
        return blacklistReadDao.getByIdCard(idcard);
    }
}
